home *** CD-ROM | disk | FTP | other *** search
/ MacHack 2000 / MacHack 2000.toast / pc / The Hacks / MacHacksBug / Python 1.5.2c1 / Extensions / Imaging / PIL / XpmImagePlugin.py < prev    next >
Encoding:
Text File  |  2000-06-23  |  2.4 KB  |  127 lines

  1. #
  2. # The Python Imaging Library.
  3. # $Id: XpmImagePlugin.py,v 1.1.1.1 1998/08/18 13:07:52 sjoerd Exp $
  4. #
  5. # XPM File handling
  6. #
  7. # History:
  8. #    96-12-29 fl    Created
  9. #
  10. # Copyright (c) Secret Labs AB 1997.
  11. # Copyright (c) Fredrik Lundh 1996.
  12. #
  13. # See the README file for information on usage and redistribution.
  14. #
  15.  
  16.  
  17. __version__ = "0.1"
  18.  
  19.  
  20. import regex, string
  21. import Image, ImageFile, ImagePalette
  22.  
  23. # XPM header
  24. xpm_head = regex.compile("\"\([0-9]*\) \([0-9]*\) \([0-9]*\) \([0-9]*\)")
  25.  
  26.  
  27. def _accept(prefix):
  28.     return prefix[:9] == "/* XPM */"
  29.  
  30.  
  31. class XpmImageFile(ImageFile.ImageFile):
  32.  
  33.     format = "XPM"
  34.     format_description = "X11 Pixel Map"
  35.  
  36.     def _open(self):
  37.  
  38.     if not _accept(self.fp.read(9)):
  39.         raise SyntaxError, "not an XPM file"
  40.  
  41.     # skip forward to next string
  42.     while 1:
  43.         s = self.fp.readline()
  44.         if not s:
  45.         raise SyntaxError, "broken XPM file"
  46.         if xpm_head.match(s) > 0:
  47.         break
  48.  
  49.     self.size = (string.atoi(xpm_head.group(1)),
  50.              string.atoi(xpm_head.group(2)))
  51.  
  52.     pal = string.atoi(xpm_head.group(3))
  53.     bpp = string.atoi(xpm_head.group(4))
  54.  
  55.     if pal > 256 or bpp != 1:
  56.         raise ValueError, "cannot read this XPM file"
  57.  
  58.     #
  59.     # load palette description
  60.  
  61.     palette = ["\0\0\0"] * 256
  62.  
  63.     for i in range(pal):
  64.  
  65.         s = self.fp.readline()
  66.         if s[-2:] == '\r\n':
  67.         s = s[:-2]
  68.         elif s[-1:] in '\r\n':
  69.         s = s[:-1]
  70.  
  71.         c = ord(s[1])
  72.         s = string.split(s[2:-2])
  73.  
  74.         for i in range(0, len(s), 2):
  75.  
  76.         if s[i] == "c":
  77.  
  78.             # process colour key
  79.             rgb = s[i+1]
  80.             if rgb == "None":
  81.             self.info["transparency"] = c
  82.             elif rgb[0] == "#":
  83.             # FIXME: handle colour names (see ImagePalette.py)
  84.             rgb = string.atoi(rgb[1:], 16)
  85.             palette[c] = chr((rgb >> 16) & 255) +\
  86.                      chr((rgb >> 8) & 255) +\
  87.                      chr(rgb & 255)
  88.             else:
  89.             # unknown colour
  90.             raise ValueError, "cannot read this XPM file"
  91.             break
  92.  
  93.         else:
  94.  
  95.         # missing colour key
  96.         raise ValueError, "cannot read this XPM file"
  97.  
  98.     self.mode = "P"
  99.     self.palette = ImagePalette.raw("RGB", string.join(palette, ""))
  100.  
  101.     self.tile = [("raw", (0, 0)+self.size, self.fp.tell(), ("P", 0, 1))]
  102.  
  103.     def load_read(self, bytes):
  104.  
  105.     #
  106.     # load all image data in one chunk
  107.  
  108.     xsize, ysize = self.size
  109.  
  110.     s = [None] * ysize
  111.  
  112.     for i in range(ysize):
  113.         s[i] = string.ljust(self.fp.readline()[1:xsize+1], xsize)
  114.  
  115.     self.fp = None
  116.  
  117.     return string.join(s, "")
  118.  
  119. #
  120. # Registry
  121.  
  122. Image.register_open("XPM", XpmImageFile, _accept)
  123.  
  124. Image.register_extension("XPM", ".xpm")
  125.  
  126. Image.register_mime("XPM", "image/xpm")
  127.